import { UI_Listen, UI_Layer } from '../../config/ui_def'
import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, OPPO_AD_CONFIG } from '../../frame/constants';
cc.Class({
    extends: cc.Component,

    properties: {
        icon: cc.Sprite,
        nameLab: cc.Label,
        priceLab: cc.Label,
        haveGetNode: cc.Node,
        adPos: cc.Node,
        numLab: cc.Label,
        closeNode:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initInfo(info, index, status) {
        if (this.adPos.getChildByName('adIcon')) {
            this.adPos.getChildByName('adIcon').getComponent('adIcon').clear()
            he.pool.putElement('adIcon', this.adPos.getChildByName('adIcon'))
            this.adPos.active = false
        }
        this.icon.sizeMode = cc.Sprite.SizeMode.TRIMMED;
        this.nameLab.node.active = true
        this.numLab.node.active = true
        this.priceLab.node.active = true
        this.haveGetNode.active = false
        this.closeNode.active=false
        this.node.active = true
        if (info) {
            this.info = info
        }
        if (index >= 0) {
            this.index = index
        }
        this.canClick = true
        if (status) {
            this.isAd = true
            this.node.active=false
            this.adSwitch(status)
            return
        } else {
            this.node.active=true
            this.isAd = false
        }
        if (this.info.isGet) {
            this.haveGetNode.active = true
        } else {
            this.haveGetNode.active = false
        }

        if (this.info.type == 1) {
            this.icon.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.id}`)
        } else if (this.info.type == 3) {
            this.icon.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.icon}`)
        }
        this.nameLab.string = `${this.info.name}`
        this.numLab.string = `x${this.info.num}`
        this.priceLab.string = this.info.diamond * this.info.num
    },

    clickServer() {
        if (this.isAd) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    // console.log('clockWXXXXXXX')
                    this.wxClilkServer()
                    break
                case Sdk_Platform.VIVO:
                    he.ad.clickNative(VIVO_AD_CONFIG.nativeAd[3])
                    break
                case Sdk_Platform.OPPO:
                    break
            }
        } else {
            if (this.canClick) {
                gg.eventManager.emit('shop', { index: this.index, type: UI_Listen.SHOP_CLICK })
            }
        }
        // if (this.canClick) {
        // } else {
        // }
    },
    wxClilkServer() {
        if (wx.tmSDK && this.canClick) {
            this.canClick = false
            wx.tmSDK.flowNavigate({
                positionId: this.adInfo.positionId, // 广告位id, 请先使用该id获取推广创意列表
                creativeId: +this.adInfo.creativeId,  // 传入获取到的creativeId
            }).then((newList) => {
                // console.log('跳转成功or取消跳转（可根据特有返回值区分两种情况）')
                // console.log('自动刷新列表：', newList)//返回最新列表 
                this.canClick = true
            }).catch((error) => {
                // console.log('跳转失败', error);
                this.canClick = true
            })
        }
    },

    adSwitch(status) {
        // this.node.active = true
        if (status) {
            this.adPos.active = true
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[3], this.creatAdIcon.bind(this))
                    break
                case Sdk_Platform.VIVO:
                    let guide = he.local.getJson('guideList', 'user')
                    if (!guide[4]) {

                    } else {
                        this.vivoAdInit()
                    }
                    break
                case Sdk_Platform.OPPO:
                    break
                default:
                    this.node.active = false
                    break
            }
        } else {
            //  关掉广告
        }
    },

    creatAdIcon(config) {
        if (config.isOpen) {
            let info = config.creatives[0]
            this.adInfo = info
            this.loadImg(this.icon, info.show_config.image)
            // this.nameLab.string = `${adInfo.desc}`
            this.nameLab.node.active = false
            this.priceLab.string = `${info.show_config.title}`
        }
    },

    vivoAdInit() {
        let adInfo = he.ad.getNativeJson(VIVO_AD_CONFIG.nativeAd[3], this.successServer.bind(this), this.failServer.bind(this))
        if (!adInfo) {
            this.failServer()
        } else if (adInfo === true) {

        } else if (adInfo) {
            this.successServer(adInfo)
        }

    },

    successServer(res) {
        if (res == [] || res == null || res == undefined) {
            this.node.active = false
        } else {
            this.node.active = true
            this.loadImg(this.icon, res.imgUrlList[0])
            this.closeNode.active=true
            this.nameLab.string = `${res.desc}`
            // console.log(this.nameLab.string)
            this.priceLab.string = `${res.title}`
        }

    },

    failServer() {
        this.node.active = false
    },

    loadImg(container, url) {
        if (url.length < 4) {
            return he.error(("图片长度不对错误"));
        }
        let ext = url.substring(url.length - 4, url.length)
        ext = ext.toLowerCase()
        if (ext == 'jpeg') ext = '.jpg'
        cc.assetManager.loadRemote(url, { ext: ext }, (err, texture) => {
            if (err) {
                he.error(err)
            }
            if (this.callFun) {
                this.callFun()
            }
            var sprite = new cc.SpriteFrame(texture);
            container.spriteFrame = sprite;
            this.icon.node.setContentSize(200, 200)
        });
    },
});
